package com.gemme.gemme.login;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.MyOSSUtils;
import com.gemme.gemme.util.PCUtil;
import com.gemme.gemme.util.SPUtil;
import com.gemme.gemme.view.MyButton;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/*
* 首次登录成功进入个人资料
* */
public class PersonInfoActivity extends AppCompatActivity implements View.OnClickListener {
   Context context =  PersonInfoActivity.this;
    ImageView iv_head;
    TextView tv_head;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        initView();
    }
    EditText et_name;
    EditText et_yqm;
    private void initView() {
        MyButton bt_confirm = findViewById(R.id.bt_confirm);
        bt_confirm.setSatate(true);

        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context,MainActivity.class));
                finish();
            }
        });

        findViewById(R.id.tv_skip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context,MainActivity.class));
                finish();
            }
        });


       iv_head = findViewById(R.id.iv_head);

        tv_head = findViewById(R.id.tv_head);

        et_name = findViewById(R.id.et_name);

        et_yqm = findViewById(R.id.et_yqm);
        iv_head.setOnClickListener(this);
        tv_head.setOnClickListener(this);
        bt_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_head:
                PCUtil.selectPic(this,1,onResultCallbackListener);
                break;

            case R.id.tv_head:
                PCUtil.selectPic(this,1,onResultCallbackListener);
                break;
            case R.id.bt_confirm:
                submitData();
                break;
        }
    }

    private void submitData() {
        submitHeadPicture();
    }

    String headPath;
    OnResultCallbackListener onResultCallbackListener = new OnResultCallbackListener(){

        @Override
        public void onResult(List result) {
            System.out.println("");

            headPath = ((LocalMedia) result.get(0)).getCompressPath();
            Glide.with(context).load(headPath).transform(new GlideCircleTransform(context))
                    .into(iv_head);
//            submitHeadPicture();





        }

        @Override
        public void onCancel() {
            System.out.println("");
        }
    };
    String http_head_url;
    private void submitHeadPicture() {
        long l = System.currentTimeMillis();
        String mPicturePath = headPath;
        if (!TextUtils.isEmpty(mPicturePath) && (new File(mPicturePath).exists())){
//            String objectName =  new File(mPicturePath).getName().replace(".",l+".");
            String objectName =  new File(mPicturePath).getName().replace(".",l+".");
             objectName = MyOSSUtils.getFileName(objectName, 2, 0);
            MyOSSUtils.getInstance().upImage(context, new MyOSSUtils.OssUpCallback() {
                @Override
                public void successImg(String img_url) {

                    http_head_url = img_url;
                    System.out.println("");
                    toServer();
                }

                @Override
                public void successVideo(String video_url) {
                    System.out.println("");
                }

                @Override
                public void inProgress(long progress, long zong) {
                    System.out.println("");
                }
            }, objectName, mPicturePath);
        }else{//没有图片地址直接上传
            toServer();
        }

    }

    private void toServer() {
//        http_head_url
        String name_str = et_name.getText() + "";
        String registrationID = JPushInterface.getRegistrationID(this);
        String yqm_str = et_yqm.getText() + "";

        //兴趣爱好,性别,生日  等于"-1"  的话就不用上传了
        String interestStr = SPUtil.getDataForString(this, Constant.INTEREST, Constant.type_qiyong);
        String user_sex = SPUtil.getDataForString(this, Constant.user_sex, Constant.type_qiyong);
        String user_birthDay = SPUtil.getDataForString(this, Constant.user_birthDay, Constant.type_qiyong);

        if(TextUtils.isEmpty(name_str)){
            return;
        }

        if(TextUtils.isEmpty(registrationID)){
            return;
        }
//PersonInfoBean
        /*
Uri.Builder builder = Uri.parse(url).buildUpon();
builder.appendQueryParameter("key", value);*/
        /*String url = DataManager.BaseUrl + ":9999/user/userapp/edituser?" +
                "nickname=" +name_str +
                "deviceNo=" + registrationID ;*/

        /* "headImgUrl=" +
                "birthday=" +
                "sex=" +
                "hobby=" +
                "invite=" +

                ;*/
        Uri.Builder builder = Uri.parse(DataManager.BaseUrl + "user/userapp/edituser?").buildUpon();
        if(!TextUtils.isEmpty(http_head_url))
            builder.appendQueryParameter("headImgUrl", http_head_url);
        builder.appendQueryParameter("nickname", name_str);
        if(!TextUtils.isEmpty(yqm_str))
            builder.appendQueryParameter("invite", yqm_str);


        if(!TextUtils.isEmpty(interestStr) && !Constant.type_qiyong.equals(interestStr))
            builder.appendQueryParameter("hobby", interestStr);
        if(!TextUtils.isEmpty(user_birthDay) && !Constant.type_qiyong.equals(user_birthDay))
            builder.appendQueryParameter("birthday", user_birthDay);
         if(!TextUtils.isEmpty(user_sex) && !Constant.type_qiyong.equals(user_sex))
                builder.appendQueryParameter("sex", user_sex);





        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(context));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(context, builder.toString(), DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    startActivity(new Intent(context, MainActivity.class));
                    System.out.println();
                    finish();

                }catch (Exception e){

                }
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


}